﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pub;
using JT_DBMgr;

namespace LZQXT.Test.Forms.DB
{
    public partial class FDBLogin : Form
    {
        CDBTree dbCon = new CDBTree();
        string sDBCon = "";

        public FDBLogin()
        {
            InitializeComponent();
        }

        public void ShowDialog(ref string sDBCon)
        {
            ShowDialog();

            sDBCon = this.sDBCon;
        }

        public void ShowDialog(ref string sDBCon, ref CDB.DBInfo dbInf)
        {
            ShowDialog();

            sDBCon = this.sDBCon;
            dbInf.sSourc = cboxServer.Text;
            dbInf.sDBNam = cboxDataBase.Text;
            dbInf.sUser_ = tboxUser.Text;
            dbInf.sPassW = tboxPass.Text;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.sDBCon = "Data Source=" + cboxServer.Text
                + ";Initial Catalog=" + cboxDataBase.Text
                + ";Persist Security Info=True;User ID=" + tboxUser.Text
                + ";Password=" + tboxPass.Text;
            //Program.fdb_tree.ado1.S_Con = this.sDBCon;

            dbCon.cmd1("UPDATE T_DBCon SET [Last]='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' "
                + "WHERE [Server]='" + cboxServer.Text + "'");

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FDBLogin_Load(object sender, EventArgs e)
        {
            cboxServer.Items.Clear();
            for (int i = 0; i < dbCon.lstDBInf.Count; i++)
            {
                Boolean b_find = false;
                for (int j = 0; j < cboxServer.Items.Count; j++)
                {
                    if (cboxServer.Items[j].ToString() == dbCon.lstDBInf[i].sServ)
                    {
                        b_find = true;
                        break;
                    }
                }
                if (!b_find)
                    cboxServer.Items.Add(dbCon.lstDBInf[i].sServ);
            }

            if (cboxServer.Items.Count > 0) cboxServer.SelectedIndex = 0;
        }

        private void FDBLogin_Activated(object sender, EventArgs e)
        {

        }

        private void cboxServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sServ = cboxServer.Text;

            cboxDataBase.Items.Clear();
            List<string> lstDBNamS = new List<string>();
            lstDBNamS = dbCon.getDBNam(sServ);
            for (int i = 0; i < lstDBNamS.Count; i++)
            {
                cboxDataBase.Items.Add(lstDBNamS[i]);
            }

            if (cboxDataBase.Items.Count > 0) cboxDataBase.SelectedIndex = 0;

            tboxUser.Text = dbCon.getDBInfo(sServ)[0].sUser;
            tboxPass.Text = dbCon.getDBInfo(sServ)[0].sPass;
        }
    }
}
